package controller.KeyListeners;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.HashMap;

import javax.swing.KeyStroke;

import controller.Controller;
import model.command.*;
import model.map.Direction;

public class DirectionalKeyListener extends KeyboardListener{
        private DirectionalCommand command;
        private Controller controller;
        private KeyListenerSet kls;
	public DirectionalKeyListener(Controller controller, KeyListenerSet listeners){
                setController(controller);
                setKLS(listeners);
		keyBindings.put(KeyStroke.getKeyStroke(97,0), new Action(){
			//TODO
			//Code for SW input for directional command
			public void perform() {
                            exe(Direction.SW);
			}
		});
		keyBindings.put(KeyStroke.getKeyStroke(98,0), new Action(){
			//TODO
			//Code for S input for directional command
			public void perform() {
                            exe(Direction.S);
			}
		});
		keyBindings.put(KeyStroke.getKeyStroke(99,0), new Action(){
			//TODO
			//Code for SE input for directional command
			public void perform() {
                            exe(Direction.SE);
			}
		});
		keyBindings.put(KeyStroke.getKeyStroke(101,0), new Action(){
			//TODO
			//Code for Focus input for directional command
			public void perform() {
                                if(kls.isLocked()){
                                    kls.unlock();
                                }
                                else{
                                    int locks[] = {2};
                                    kls.lock(locks);
                                }
			}
		});
		keyBindings.put(KeyStroke.getKeyStroke(103,0), new Action(){
			//TODO
			//Code for NW input for directional command
			public void perform() {
                            exe(Direction.NW);
			}
		});
		keyBindings.put(KeyStroke.getKeyStroke(104,0), new Action(){
			//TODO
			//Code for N input for directional command
			public void perform() {
                            exe(Direction.N);
			}
		});
		keyBindings.put(KeyStroke.getKeyStroke(105,0), new Action(){
			//TODO
			//Code for NE input for directional command
			public void perform() {
                            exe(Direction.NE);
			}
		});
	}
        private void exe(Direction direction){
        	DirectionalCommand temp = (DirectionalCommand)((Command)command).clone();
        	command.setDirection(direction);
        	getController().queueCommandInPlayable((Command) command);
        	setCommand(temp);
        }
        private Controller getController(){
            return controller;
        }
        /**
         * Sets the command for this ket listener to fire.
         * 
         * @param command
         */
        public void setCommand(DirectionalCommand command){
            this.command = command;
        }
        public void setController(Controller controller){
            this.controller = controller;
        }
        private void setKLS(KeyListenerSet kls){
            this.kls = kls;
        }
}
